package com.xmb.wechat.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.blankj.utilcode.util.ScreenUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xmb.wechat.R;
import com.xmb.wechat.bean.BankCardBean;
import com.xmb.wechat.contact.InitContact;
import com.xmb.wechat.definterface.onBankCardChooseCallBack;
import com.xmb.wechat.delegate.BankListDelegate;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/17 10:44
 */
public class BankDialog extends Dialog {

    private BankListDelegate mDelegate;

    public BankDialog(@NonNull Context context, onBankCardChooseCallBack callBack) {
        super(context, R.style.PopupDialog);
        initView(context, callBack);
    }

    private void initView(Context context, onBankCardChooseCallBack callBack) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_bank, null, false);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        mDelegate = new BankListDelegate(context, recyclerView);
        mDelegate.setOnItemClick(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String item = (String) mDelegate.getData().get(position);
                BankCardBean bankCardBean = new BankCardBean();
                //银行卡简称
                bankCardBean.setBankName(item);
                //银行卡图片
                if (position < InitContact.bankIcons.length)
                    bankCardBean.setAvatar("file:///android_asset/banks/" + InitContact.bankIcons[position]);
                //银行卡全称
                if (position < InitContact.bankIcons.length)
                    bankCardBean.setFullBankName(InitContact.fullBankNames[position]);
                if (callBack != null)
                    callBack.onClick(bankCardBean);
                dismiss();
            }
        });
        setContentView(view);
        WindowManager.LayoutParams lp = Objects.requireNonNull(getWindow()).getAttributes();
        lp.dimAmount = 0.5f;
        lp.width = ScreenUtils.getScreenWidth() * 4 / 5;
        lp.height = ScreenUtils.getScreenHeight() * 4 / 5;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        //添加数据
        initData();
    }

    private void initData() {
        mDelegate.addData(Arrays.asList(InitContact.banks));
    }
}
